﻿using Iworld.Backstage.Framework;
using Iworld.Backstage.Framework.AdministratorService;
using Iworld.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage
{
    public partial class CreateGroupWindow : ChildWindow
    {
        public CreateGroupWindow()
        {
            this.DataContext = new AdminGroupMode();
            InitializeComponent(); 
        }

        void ShowMsg(string msg)
        {
            new ErrorPrompt() { Info = msg }.Show();
        }

        public AdministratorGroupExport Info
        {
            get { return (AdministratorGroupExport)GetValue(InfoProperty); }
            set { SetValue(InfoProperty, value); }
        }


        public static readonly DependencyProperty InfoProperty =
            DependencyProperty.Register("Info", typeof(AdministratorGroupExport), typeof(CreateGroupWindow), new PropertyMetadata(null, (sender, args) => {
                AdministratorGroupExport am = (AdministratorGroupExport)args.NewValue;
                CreateGroupWindow win = sender as CreateGroupWindow;
                win.DataContext = new AdminGroupMode() {
                    Name = am.Name, 
                    CanCreateRechargeCard =am.CanCreateRechargeCard,
                    CanEditAdministrator = am.CanEditAdministrator,
                    CanEditAdministratorGroup = am.CanEditAdministratorGroup,
                    CanEditGameRound = am.CanEditGameRound,
                    CanEditGameSetting = am.CanEditGameSetting,
                    CanEditUser = am.CanEditUser,
                    CanViewGameChapter = am.CanViewGameChapter,
                    CanViewGameRound = am.CanViewGameRound
                   ,CanReviewWithdrawals = am.CanReviewWithdrawals
                };
            }));

        
        private void Enter(object sender, RoutedEventArgs e)
        {
            AdminGroupMode am = this.DataContext as AdminGroupMode;
            AdministratorServiceClient client = new AdministratorServiceClient();
             try
                { 
                        if (Info == null)
                        {
                            client.CreateAdministratorGroupCompleted += client_CreateAdministratorGroupCompleted;
               
                                client.CreateAdministratorGroupAsync(new CreateAdministratorGroupImport()
                                {
                                    Name = am.Name,
                                    CanCreateRechargeCard = (bool)am.CanCreateRechargeCard,
                                    CanEditAdministrator = (bool)am.CanEditAdministrator,
                                    CanEditAdministratorGroup = (bool)am.CanEditAdministratorGroup,
                                    CanEditGameRound = (bool)am.CanEditGameRound,
                                    CanEditGameSetting = (bool)am.CanEditGameSetting,
                                    CanEditUser = (bool)am.CanEditUser,
                                    CanViewGameChapter = (bool)am.CanViewGameChapter,
                                    CanViewGameRound = (bool)am.CanViewGameRound,
                                    CanReviewWithdrawals = (bool)am.CanReviewWithdrawals,
                                    Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
                                });
                  
                        }
                        else
                        {
                            client.EditAdministratorGroupCompleted += client_EditAdministratorGroupCompleted;
                            client.EditAdministratorGroupAsync(new EditAdministratorGroupImport() {
                                Id=Info.Id,
                                Name = am.Name,
                                CanCreateRechargeCard = (bool)am.CanCreateRechargeCard,
                                CanEditAdministrator = (bool)am.CanEditAdministrator,
                                CanEditAdministratorGroup = (bool)am.CanEditAdministratorGroup,
                                CanEditGameRound = (bool)am.CanEditGameRound,
                                CanEditGameSetting = (bool)am.CanEditGameSetting,
                                CanEditUser = (bool)am.CanEditUser,
                                CanViewGameChapter = (bool)am.CanViewGameChapter,
                                CanViewGameRound = (bool)am.CanViewGameRound,
                                CanReviewWithdrawals=(bool)am.CanReviewWithdrawals,
                                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
                            });
                        }
              }
             catch
             {
                 ShowMsg("请选择完整信息");
             } 
           
        }

        void client_EditAdministratorGroupCompleted(object sender, EditAdministratorGroupCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                ShowMsg("编辑成功");
                this.DialogResult = true;
            }
            else
            {
                ShowMsg(e.Result.Error);
            }
        }

        void client_CreateAdministratorGroupCompleted(object sender, CreateAdministratorGroupCompletedEventArgs e)
        {
          if(e.Result.Success)
          {
              ShowMsg("添加成功");
              this.DialogResult = true;
          }
            else
          {
              ShowMsg(e.Result.Error);
          }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void AllYes(object sender, RoutedEventArgs e)
        {
            AdminGroupMode am = this.DataContext as AdminGroupMode;
            am.CanCreateRechargeCard = true;
            am.CanEditAdministratorGroup = true;
            am.CanEditAdministrator = true;
            am.CanEditGameRound = true;
            am.CanEditGameSetting = true;
            am.CanEditUser = true;
            am.CanViewGameChapter = true;
            am.CanViewGameRound = true;
            am.CanReviewWithdrawals = true;
        }

        private void AllNo(object sender, RoutedEventArgs e)
        {
            AdminGroupMode am = this.DataContext as AdminGroupMode;
            am.CanCreateRechargeCard = false;
            am.CanEditAdministratorGroup = false;
            am.CanEditAdministrator = false;
            am.CanEditGameRound = false;
            am.CanEditGameSetting = false;
            am.CanEditUser = false;
            am.CanViewGameChapter = false;
            am.CanViewGameRound = false;
            am.CanReviewWithdrawals = false;
        }
    }
    #region 类型
    public class AdminGroupMode : ModelBase
    {
        string name;
        bool? canEditGameRound = false;
        bool? canViewGameRound = false;
        bool? canViewGameChapter = false;
        bool? canEditUser = false;
        bool? canEditGameSetting = false;
        bool? canCreateRechargeCard = false;
        bool? canEditAdministrator = false;
        bool? canEditAdministratorGroup = false;
        bool? canReviewWithdrawals = false;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public bool? CanReviewWithdrawals
        {
            get
            {
                return canReviewWithdrawals;
            }
            set
            {
                if (canReviewWithdrawals != value)
                {
                    canReviewWithdrawals = value;
                    OnPropertyChanged("CanReviewWithdrawals");
                }
            }
        }

        /// <summary>
        /// 能否编辑牌局
        /// </summary>
        public bool? CanEditGameRound
        {
            get
            {
                return canEditGameRound;
            }
            set
            {
                if (canEditGameRound != value)
                {
                    canEditGameRound = value;
                    OnPropertyChanged("CanEditGameRound");
                }
            }
        }
        /// <summary>
        /// 能否查看牌局信息
        /// </summary>
        public bool? CanViewGameRound
        {
            get
            {
                return canViewGameRound;
            }
            set
            {
                if (canViewGameRound != value)
                {
                    canViewGameRound = value;
                    OnPropertyChanged("CanViewGameRound");
                }
            }
        }

        /// <summary>
        /// 是否能查看牌轮信息
        /// </summary>
        public bool? CanViewGameChapter
        {
            get
            {
                return canViewGameChapter;
            }
            set
            {
                if (canViewGameChapter != value)
                {
                    canViewGameChapter = value;
                    OnPropertyChanged("CanViewGameChapter");
                }
            }
        }

        /// <summary>
        /// 能否编辑前台用户
        /// </summary>
        public bool? CanEditUser
        {
            get
            {
                return canEditUser;
            }
            set
            {
                if (canEditUser != value)
                {
                    canEditUser = value;
                    OnPropertyChanged("CanEditUser");
                }
            }
        }

        /// <summary>
        /// 能否编辑游戏设置
        /// </summary>
        public bool? CanEditGameSetting
        {
            get
            {
                return canEditGameSetting;
            }
            set
            {
                if (canEditGameSetting != value)
                {
                    canEditGameSetting = value;
                    OnPropertyChanged("CanEditGameSetting");
                }
            }
        }

        /// <summary>
        /// 能否生成充值卡
        /// </summary>
        public bool? CanCreateRechargeCard
        {
            get
            {
                return canCreateRechargeCard;
            }
            set
            {
                if (canCreateRechargeCard != value)
                {
                    canCreateRechargeCard = value;
                    OnPropertyChanged("CanCreateRechargeCard");
                }
            }
        }

        /// <summary>
        /// 能否编辑管理员
        /// </summary>
        public bool? CanEditAdministrator
        {
            get
            {
                return canEditAdministrator;
            }
            set
            {
                if (canEditAdministrator != value)
                {
                    canEditAdministrator = value;
                    OnPropertyChanged("CanEditAdministrator");
                }
            }
        }

        /// <summary>
        /// 能否编辑管理员用户组
        /// </summary>
        public bool? CanEditAdministratorGroup
        {
            get
            {
                return canEditAdministratorGroup;
            }
            set
            {
                if (canEditAdministratorGroup != value)
                {
                    canEditAdministratorGroup = value;
                    OnPropertyChanged("CanEditAdministratorGroup");
                }
            }
        }


    }
    #endregion
    
}

